package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.bo.MarketIssueBo;
import com.cskaoyan.model.admin.vo.MarketIssueVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/11/30 15:55
 */
@Service
public class AdminIssueServiceImpl implements AdminIssueService {

    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public List<MarketIssueVo> list(BaseParam param,String question) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<MarketIssue> marketIssueList = marketIssueMapper.selectList(question);
        List<MarketIssueVo> marketIssueVoList=new ArrayList<>();
        for (MarketIssue marketIssue : marketIssueList) {
            Boolean deleted;
            if(marketIssue.getDeleted().equals(0)){
                deleted=true;
            }else{
                deleted=false;
            }
            MarketIssueVo marketIssueVo=new MarketIssueVo(deleted,marketIssue.getQuestion(),marketIssue.getAnswer(),marketIssue.getAddTime(),marketIssue.getUpdateTime(),marketIssue.getId());
            marketIssueVoList.add(marketIssueVo);
        }
        return marketIssueVoList;
    }

    @Override
    public void deleteMarketIssue(int id) {
        marketIssueMapper.deleteMarketIssueById(id);
    }

    @Override
    public void updateMarketIssue(MarketIssueBo marketIssueBo) {
        MarketIssue marketIssue=new MarketIssue();
        marketIssue.setAddTime(marketIssueBo.getAddTime());
        marketIssue.setAnswer(marketIssueBo.getAnswer());
        marketIssue.setDeleted(marketIssueBo.isDeleted());
        marketIssue.setId(marketIssueBo.getId());
        marketIssue.setQuestion(marketIssueBo.getQuestion());
        marketIssue.setUpdateTime(marketIssueBo.getUpdateTime());
        marketIssueMapper.updateMarketIssue(marketIssue);
    }

    @Override
    public MarketIssueVo createMarketIssue(MarketIssueBo marketIssueBo) {
        MarketIssue marketIssue=new MarketIssue();
        marketIssue.setQuestion(marketIssueBo.getQuestion());
        marketIssue.setAnswer(marketIssueBo.getAnswer());
        marketIssueMapper.createMarketIssue(marketIssue);
        MarketIssueVo marketIssueVo=marketIssueMapper.selectIssueById(marketIssue.getId());
        return marketIssueVo;
    }
}
